import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: xiaotutu
 * Date: 2024-09-04
 * Time: 16:33
 */
public class Test {
    // 栈的压入 弹出序列
    public boolean IsPopOrder (int[] pushV, int[] popV) {
        // write code here
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for(int i = 0; i < pushV.length; i++) {
            stack.push(pushV[i]);
            while(!stack.empty() && j < popV.length && stack.peek() == popV[j]) {
                stack.pop();
                j++;
            }
        }
        return stack.empty();
    }

    public static int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(String s : tokens) {
            if(!isOpera(s)) {
                // 不是操作符 入栈
                stack.push(Integer.parseInt(s));
            }else {
                int num2 = stack.pop();
                int num1 = stack.pop();
                switch(s) {
                    case "+":
                        stack.push(num1 + num2);
                        break;
                    case "-":
                        stack.push(num1 - num2);
                        break;
                    case "*":
                        stack.push(num1 * num2);
                        break;
                    case "/":
                        stack.push(num1 / num2);
                        break;
                }
            }
        }
        return stack.pop();
    }

    public static boolean isOpera(String s) {
        if (s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        String[] tokens = new String[]{"2","1","+","3","*"};
        System.out.println(evalRPN(tokens));
    }
    // 逆波兰表达式求值
//    public int evalRPN1(String[] tokens) {
//        Stack<Integer> stack = new Stack<>();
//        for(String s : tokens) {
//            if(!isOpera1(s)) {
//                stack.push(Integer.parseInt(s));
//            }else {
//                int num2 = stack.pop();
//                int num1 = stack.pop();
//                switch(s) {
//                    case "+":
//                        stack.push(num1 + num2);
//                        break;
//                    case "-":
//                        stack.push(num1 - num2);
//                        break;
//                    case "*":
//                        stack.push(num1 * num2);
//                        break;
//                    case "/":
//                        stack.push(num1 / num2);
//                        break;
//                }
//            }
//        }
//        return stack.pop();
//    }
//
//    public boolean isOpera1(String s) {
//        if(s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")) {
//            return true;
//        }else {
//            return false;
//        }
//    }
    // 括号匹配
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for(int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if(ch == '(' || ch == '[' || ch == '{') {
                stack.push(ch);
            }else {
                // 是右括号 需要匹配
                if(stack.empty()) {
                    return false;
                }else {
                    char tmp = stack.peek();
                    if(tmp == '(' && ch == ')' || tmp == '[' && ch == ']' || tmp == '{' && ch == '}') {
                        stack.pop();
                    }else {
                        return false;
                    }
                }
            }
        }
        if(!stack.empty()) {
            return false;
        }
        return true;
    }

    /*public static void main(String[] args) {
        int[] array = new int[]{1, 2, 3, 4, 5};
        for (int i = 0; i < array.length; i++) {

        }

        String s = "abcdef";
        for (int i = 0; i < s.length(); i++) {


        }
    }*/
}
